var UPLOAD_URL;

var MUTIPLE_UPLOAD = {
    holderplace: '#selQueue',
    swfu: '',
    completed: false,
    submit_btn: '',
    remove_btn: '#btnCancelSelectedFile',
    fileDialogComplete : function (numFilesSelected, numFilesQueued) {
        try {
            $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].options.length] = new Option("File Dialog Complete: " + numFilesSelected + ", " + numFilesQueued, "");
        } catch (ex) {
            this.debug(ex);
        }
    },
    fileQueued : function (file) {
        //        alert(MUTIPLE_UPLOAD.holderplace);
        try {
            var queueString = file.name +  ":  0%" ;
            $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].options.length] = new Option(queueString, file.id);
        //                        FeaturesDemo.selEventsQueue.options[FeaturesDemo.selEventsQueue.options.length] = new Option("File Queued: " + file.id, "");
        } catch (ex) {
            this.debug(ex);
        }
    },
    uploadStart : function () {
        var file_id =$(MUTIPLE_UPLOAD.holderplace).val();
        //if(!file_id) return;
        $(MUTIPLE_UPLOAD.remove_btn)[0].disabled = true;
        this.swfu.startUpload(file_id);
    },
    uploadProgress : function (file, bytesLoaded, totalBytes) {
        try {
            var percent = Math.ceil((bytesLoaded / file.size) * 100);
            if (percent < 10) {
                percent = "  " + percent;
            } else if (percent < 100) {
                percent = " " + percent;
            }
            $(MUTIPLE_UPLOAD.holderplace).val(file.id);
            var queueString = file.name + ' 上传:' + percent + '%';
            $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = queueString;

            $('#progress').html('<div class="progressName">' +file.name+ '</div><div class="progressBarStatus">uploading...</div><div class="progressBarInProgress" style="width:' +percent+ '%;"/>');
        } catch (ex) {
            this.debug(ex);
        }
    },
    uploadSuccess : function (file, serverData) {
        try {
            var queueString = file.name + ":上传成功";
            $(MUTIPLE_UPLOAD.holderplace)[0].value = file.id;
            $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = queueString;

            $('#done').append('<div class="progressName">' +file.name+ '</div><div class="progressBarStatus">Upload completed.</div>');

        //                  FeaturesDemo.selEventsFile.options[FeaturesDemo.selEventsFile.options.length] = new Option("Upload Success: " + file.id, "");

        //                 FeaturesDemo.divServerData.innerHTML = typeof(serverData) === "undefined" ? "" : serverData.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/\t/g, "    ").replace(/  /g, " &nbsp;");
        } catch (ex) {
            this.debug(ex);
        }
    },
    uploadComplete : function (file) {
    //$('#MSG').append(file.id+'<br />');
    //alert(file.filestatus == SWFUpload.FILE_STATUS.COMPLETE);
    //       if(file.filestatus != SWFUpload.FILE_STATUS.COMPLETE) {
    //          return false;
    //       }
    },
    uploadError : function (file, errorCode, message) {
        //alert(SWFUpload.UPLOAD_ERROR.IO_ERROR);
        try {
            var errorName = "";
            switch (errorCode) {
                case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                    $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = file.id + ":HTTP:" + file.name;
                    errorName = "HTTP ERROR";
                    break;
                case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
                    errorName = "MISSING UPLOAD URL";
                    break;
                case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                    $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = file.id + ":IO  :" + file.name;
                    errorName = "IO ERROR";
                    break;
                case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                    $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = file.id + ":SEC :" + file.name;
                    errorName = "SECURITY ERROR";
                    break;
                case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                    errorName = "UPLOAD LIMIT EXCEEDED";
                    break;
                case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
                    errorName = "UPLOAD FAILED";
                    break;
                case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
                    errorName = "SPECIFIED FILE ID NOT FOUND";
                    break;
                case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
                    errorName = "FILE VALIDATION FAILED";
                    break;
                case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                    errorName = "FILE CANCELLED";

                    $(MUTIPLE_UPLOAD.holderplace)[0].value = file.id;
                    $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = file.id + ":----:" + file.name;

                    FeaturesDemo.selEventsFile.options[FeaturesDemo.selEventsFile.options.length] = new Option("File Cancelled " + file.id, "");
                    break;
                case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                    errorName = "FILE STOPPED";

                    $(MUTIPLE_UPLOAD.holderplace)[0].value = file.id;
                    $(MUTIPLE_UPLOAD.holderplace)[0].options[$(MUTIPLE_UPLOAD.holderplace)[0].selectedIndex].text = file.id + ":  0%:" + file.name;

                    FeaturesDemo.selEventsFile.options[FeaturesDemo.selEventsFile.options.length] = new Option("File Stopped " + file.id, "");
                    break;
                default:
                    errorName = "UNKNOWN";
                    break;
            }
        } catch (ex) {
            this.debug(ex);
        }
    },
    stopUpload: function () {
        this.swfu.stopUpload();
        return true;
    },
    cancelSelectedFile: function (triggerEvent) {
        var file_id = $(MUTIPLE_UPLOAD.holderplace).val();
        this.swfu.cancelUpload(file_id, triggerEvent);
        
        $(this.holderplace).find('option[value="' +file_id+ '"]').remove();
        return true;
    },
    removeSelecedFile: function() {
        $('#progress').html('');
    }
    ,
    queueComplete: function() {
        $('#progress').html('');
    //    $(MUTIPLE_UPLOAD.submit_btn)[0].disabled = false;
    //    $(MUTIPLE_UPLOAD.remove_btn)[0].disabled = false;

    }
}